/*
	Class: 		PANAjax
	Description: 	Sending and retrieving information from server using AJAX technology,
				written in JavaScript cross browser.
	Editor:		Even Simon < even.simon@gmail.com >
	Powered by:		Rock-Host.com Panorama project < http://panorama.rock-host.com/ >
	Usage:
		<example>
			function dodo(data){
				document.write(data);
			}

			var a = new Array();
			var b = new Array();
			var myAjax = new PANAjax();
			myAjax._url = 'text_switch.html';

			a[0] = 'ac';
			b[0] = 'getHeader';
			
			myAjax.readFile();
			
			Or
			
			myAjax.sendAndLoad(a, b, 'dodo'); "dodo" is a handler function name which recives data from request.responseText
		</example>
*/

PANAjax = function(){}	/* Create PANAjax Class */
with({$: PANAjax.prototype}){
	$.request;											/* Set request Variable */
	$._url;												/* Set request URL */
	$._handler;											/* Set return variable */
	$.Type = 'Class';									/* Set Class Type */
	$.Name = 'PANAjax Class';							/* Set Class Name */
	$.Editor = 'Even Simon <even.simon@gmail.com>';		/* Set Editor Name */
	
	/* Start Connection function (Create XML HTTP Request) */
	$.Connect = function() {
		try {
			$.request = new XMLHttpRequest();
		} catch (trymicrosoft) {
		    try {
				$.request = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch (othermicrosoft) {
				try {
					$.request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					$.request = false;
				}
		    }
		}
		if (!$.request){
			alert("Error initializing XMLHttpRequest!");
			return false;
		}
	}
	/* End Connection function */

	/* Start Handler function */
	$.handleResponse = function(){
		if ($.request.readyState == 4){
		    if ($.request.status == 200){
				window[$._handler]($.request.responseText);
			}
		}
	}
	/* End Handler function */

	/* Start sendAndLoad function */
	$.sendAndLoad = function(dat, dat_val, _hand){
		$._handler = _hand;
		$.Connect();
		url = this._url + "?";
			for(i=0;i<dat.length;i++){
				url += dat[i] + '=' + dat_val[i] + '&';
			}
		$.request.open("GET", url, true);
		$.request.onreadystatechange = $.handleResponse;
		$.request.send();	
	}
	/* End sendAndLoad function */
	
	/* Start readFile function */
	$.readFile = function(){
		$.Connect();
		url = this._url;
		$.request.open("GET", url, true);
		$.request.onreadystatechange = function(){
		if ($.request.readyState == 4){
		    if ($.request.status == 200){
				document.write($.request.responseText);
			}
		}
		}
		$.request.send();	
	}
	/* End readfile function */
}
